package com.lengmou.ctrl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.lengmou.entity.Cars;
import com.lengmou.service.CarsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cars")
public class CarsController {

    @Resource
    private CarsService carsService;

    @RequestMapping(value = "/getAll",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getAllCars(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                             Integer goodsid, Integer userid){
        Map<String,Object> map = new HashMap<>();
        map.put("goodsid",goodsid);
        map.put("userid",userid);
        JSONObject json = new JSONObject();
        PageHelper.startPage(pageNum,pageSize);
        List<Cars> search = carsService.search(map);
        json.put("code",0);
        json.put("data",search);
        json.put("count",carsService.search(map).size());
        return json.toString();
    }

//    设置购物的商品
    @RequestMapping(value = "/setCarGoods",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setCarGoods(Cars cars){
        JSONObject json = new JSONObject();
        if(cars!=null){
            int i = carsService.setCarsGoodsid(cars);
            json.put("state",i);
        }else{
//            传入的参数不存在
            json.put("state",-1);
        }

        return json.toString();
    }
    @RequestMapping(value = "/setCarUsers",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setCarUsers(Cars cars){
        JSONObject json = new JSONObject();
        if(cars!=null){
            int i = carsService.setCarsUserid(cars);
            json.put("state",i);
        }else{
//            传入的参数不存在
            json.put("state",-1);
        }

        return json.toString();
    }
    @RequestMapping(value = "/setCarNum",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setCarNum(Cars cars){
        JSONObject json = new JSONObject();
        if(cars!=null){
            int i = carsService.setCarsNum(cars);
            json.put("state",i);
        }else{
//            传入的参数不存在
            json.put("state",-1);
        }

        return json.toString();
    }

    @RequestMapping(value = "/addCar",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String addCar(Cars cars){
        JSONObject json = new JSONObject();
        if(cars!=null){
            int i = carsService.addCars(cars);
            json.put("state",i);
        }else{
//            传入的参数不存在
            json.put("state",-1);
        }

        return json.toString();
    }

    @RequestMapping(value = "/deleteAll",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String deleteAll( @RequestBody List<Cars> cars){
        JSONObject json = new JSONObject();
        if(cars!=null&&cars.size()>0){
            int i = 0;
            for (Cars o:cars) {
                i+=carsService.deleteCars(o.getId());
            }
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }





}
